<template>
  <div class="box">
    <img src="@/assets/homeLogo.webp" style="width: 100%; height: 52px" />
    <!-- 下拉搜索icon -->
    <div class="iconSelect1" @click="round">
      <div :class="{ iconActive1: this.active, iconActive: !this.active }"></div>
    </div>

    <!-- 下拉搜索功能框 -->
    <div class="selectDown" v-show="active">
      <div>
        <div class="title">全部频道</div>
        <!-- 下拉搜索图标 -->
        <div class="iconSelect" @click="round">
          <div :class="{ iconActive1: this.active, iconActive: !this.active }"></div>
        </div>
      </div>
      <!-- 下拉搜索的多个按钮 -->
      <!-- 每个按钮跳转 通过切换卡片操作   -->
      <div class="content">
        <button class="btn" @click="active = false">推荐</button>

        <button
          @click="goNav(index)"
          :class="{ select: show-1 == index }"
          class="btn"
          v-for="(nav, index) in cateList"
          :title="nav.name"
          :key="index"
        >
          {{ nav.name }}
        </button>
      </div>
    </div>
    <div class="mask" v-show="active" @click="round"></div>

    <!-- 头部 -->
    <!-- <van-sticky background="back" type="card"> -->
    <div class="header">
      <a href="#" class="logo"></a>

      <router-link to="/search">
        <div class="search">
          <van-icon name="search" class="icon" />
          <span class="text">搜索商品, 共121077款好物</span>
        </div>
      </router-link>
      <span class="loginBtn" @click="goLogin">登录</span>
      <!-- <router-link class="loginBtn" to="/login" >登录</router-link> -->
    </div>
    <!-- </van-sticky> -->
    <!-- 下拉搜索 -->
    <!-- 滚动列表 -->
    <van-tabs line-height="1px" @click="handler" v-model="show">
      <van-tab title="推荐 ">
        <!-- card组件 -->

        <Card></Card>
      </van-tab>
      <!-- <Life:category='category'></Life:category=>

       -->

      <van-tab v-for="(nav, index) in cateList" :title="nav.name" :key="index"> <Life :categoryId="nav.id"></Life></van-tab>
    </van-tabs>
  </div>
</template>
<script>
import Vue from "vue";
import { Icon, Tab, Tabs, Swipe, SwipeItem, Lazyload } from "vant";

Vue.use(Icon);
Vue.use(Swipe);
Vue.use(SwipeItem);
Vue.use(Icon);
Vue.use(Tab);
Vue.use(Tabs);
// Vue.use(Sticky);
import Card from "./components/Card.vue";
import Life from "./life/index.vue";

import { mapGetters, mapState } from "vuex";

// import VueRouter from 'vue-route'

export default {
  // 组件名字
  name: "Home",
  // 状态数据
  data() {
    return {
      value: "",
      active: false,
      //  show: false,

      currentIndex: 0,
      show: 0,
    };
  },
  components: {
    Card,
    Life,
  },
  mounted() {
    // 获取首页数
    this.getHomeData();
    this.getNavList();
  },
  methods: {
    showPopup() {
      this.show = true;
    },
    //标签点击事件 每次点击都发请求拿life里的数据
    handler(name, title) {
      let index = name - 1;
      // console.log(name);
      // console.log(this.cateList[index].id);
      this.getGoodsList(this.cateList[index].id);
    },
    // home页面头部搜索三角的回调
    round() {
      this.active = !this.active;
    },

    // 下拉切换导航页面事件
    goNav(index) {
      // this.currentIndex = index
      // let index = this.currentIndex
      // this.getGoodsList(this.cateList[index].id);
      // this.currentIndex == index;
      this.show = index + 1;
      let categoryId = this.cateList[this.show].id;
      this.active = false;
      // this.getGoodsList(categoryId)
    },

    //点击导航一个标签事件回调
    // changeIndex(index){
    // 	// console.log(123)
    // 	this.currentIndex = index
    // },
    // 跳转，去登录
    goLogin() {
      this.$router.push("/login");
    },
    // 获取home页数据
    getHomeData() {
      this.$store.dispatch("home/getHomeData");
    },
    getNavList() {
      this.$store.dispatch("home/getNavList");
    },
    // 获取导航分类下的商品数据列表
    getGoodsList(id) {
      // console.log(123);
      this.$store.dispatch("home/getGoodsList", id);
    },
  },

  computed: {
    ...mapGetters("home", ["categoryList", "cateList"]),
    // ...mapState({
    //   list: (state) => state.home.list,
    // }),

    // 导航分类列表数据
    // categoryLists(){
    //   return list.categoryL1List ||[]
    // }
  },
};
</script>
<style lang="less" scoped>
.box {
  width: 100%;
  height: 100%;
  //  position: fixed;
  //      top: 0;

  .iconSelect1 {
    width: 20px;
    margin-top: 22px;
    height: 25px;
    position: absolute;
    top: 39px;
    right: 0px;
    z-index: 99;
    background-color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    div {
      width: 15px;
      height: 15px;
      background-size: 50px 40px;
      background-image: url("../../assets/icon2.png");
    }
    .iconActive {
      transition: all 0.3s linear;
      transform: rotateZ(0deg);
    }
    .iconActive1 {
      transition: all 0.3s linear;
      transform: rotateZ(180deg);
    }
  }

  .selectDown {
    margin-top: 26px;
    float: left;
    position: absolute;
    top: 38px;
    z-index: 2;
    width: 100%;
    height: 155px;
    padding-left: 10px;
    background-color: white;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    .title {
      font-size: 12px;
      line-height: 20px;
      margin-bottom: 8px;
    }
    .content {
      flex: 1;
      .btn {
        // width: 65px;
        // height: 26px;
        width: 75px;
        height: 30px;
        font-size: 10px;
        margin: 0 8px 8px 0;
        border: 1px solid #ccc;
        background: #fafafa;
        border-radius: 2px;
      }
      .select {
        color: #dd1a21;
        border-color: #dd1a21;
      }
    }
  }

  .mask {
    position: fixed;
    z-index: 1;
    background: rgba(0, 0, 0, 0.5);
    top: 70px;
    left: 0;
    right: 0;
    bottom: 0;
  }

  .header {
    width: 100%;
    height: 44px;
    // background: pink;
    display: flex;
    align-items: center;
    box-sizing: border-box;
    padding: 8px 15px;
    .logo {
      width: 70px;
      height: 40px;
      display: inline-block;
      margin-right: 10px;
      // background-color: aqua;
      background-image: url(../../assets/02.png);
      // background-size: 100px 100px;
      // background-position: -25px 15px;
      background-size: 195px 195px;
      background-position: -65px 30px;
    }
    .search {
      height: 28px;
      display: flex;
      align-items: center;
      background-color: #ededed;
      flex: 1;
      border-radius: 5px;
      text-align: center;
      .icon {
        width: 14px;
        height: 14px;
        margin: 0px 10px 0px 15px;
        line-height: 14px;
        font-size: 20px;
      }
      .text {
        font-size: 14px;
        margin-right: 5px;
        line-height: 14px;
        text-align: center;
        color: #666;
      }
    }
    .loginBtn {
      width: 35px;
      height: 20px;
      font-size: 14px;
      border: 1px solid #dd1a21;
      border-radius: 5px;
      color: #dd1a21;
      text-align: center;
      margin-left: 8px;
      line-height: 20px;
    }
  }
  :deep(.van-tabs__wrap) {
    height: 20px;
    font-size: 12px;
  }
  :deep(.van-tab--active) {
    color: #dd1a21;
  }
}
</style>
